﻿
Imports System.IO
Imports System.Data.SqlClient
'Imports Microsoft.Reporting.WinForms
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Text.RegularExpressions

Public Class FormMain

    Dim sexid, positionid, degreeid, addressid As Int16
    Public ecode, moraf, userrequestcode As String
    Dim ImgLoc As String = ""
    Dim command As New SqlCommand
    Dim tooltip As New ToolTip
    Dim rndnumber As Random
    Dim number As Integer
    Public RanMumber As Int16

                    
    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

        FormProgressBarLoad.Close()
    End Sub



    Private Sub FormMain_HelpButtonClicked(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        FormAbout.ShowDialog()

    End Sub


    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button1.Hide()
        Button2.Hide()
        Restore.Visible = False
        RestoreAll.Visible = False

        RestoreToolStripMenuItem2.Visible = False
        RestoreAllToolStripMenuItem1.Visible = False

        'Me.DgvUserLogin.ReadOnly = False
        'For Each column As DataGridViewColumn In Me.DgvUserLogin.Columns
        '    column.ReadOnly = True
        'Next
        'Me.DgvUserLogin.Columns("PASSWORD").ReadOnly = False

        'Me.Dgvholiday.ReadOnly = False
        'Me.Dgvholiday.MultiSelect = True

        Try

            '=====================
            Me.txtDataGridViewAlternativeColor.BackColor = My.Settings.DataGridViewAlternativeColor
            Me.txtDataGridViewRowHeigt.Text = My.Settings.DataGridViweRowHeight
            Me.txtDataGridViwFontName.Font = My.Settings.DataGridViweFont
            Me.cboLanguage.Text = My.Settings.Languguage
            '=====================
            lblMessage.ForeColor = Color.White




            'Show data into ComboBox
            loadsex()
            cbosex_SelectedIndexChanged(sender, e)

            LoadPosition()
            CboPostion_SelectedIndexChanged(sender, e)

            loadDegree()
            cbodegree_SelectedIndexChanged(sender, e)

            loadAddress()
            CboAddress_SelectedIndexChanged(sender, e)
            validsymbal()
            '----------------------------------------------------
            lblID.Hide()
            lblactive.Hide()
            Panel2.BackColor = Color.White

            ecode = FormLogin.ecode
        
            LBL_Logo.Hide()
            lblUserRequestid.Hide()
            LBL_Logo.ForeColor = Color.White
            LBL_Search.ForeColor = Color.White
            Timer1_Tick(sender, e)
            LBL_Date.ForeColor = Color.White
            LBL_NAME.ForeColor = Color.White
            LBL_Employee.ForeColor = Color.White
            LBL_Attendance.ForeColor = Color.White
            lBL_Holiday.ForeColor = Color.White
            LBL_Report.ForeColor = Color.White
            LBL_Permission.ForeColor = Color.White
            LBL_UserRequest.ForeColor = Color.White
            LBL_Setting.ForeColor = Color.White
            LBL_RestoreDatabase.ForeColor = Color.White
            LBL_About.ForeColor = Color.White
            LBL_LogOut.ForeColor = Color.White

            ''''''''''''''''''''''''''''''''''''

            LBL_Date.Text = "Today is  " + Now.ToLongDateString
            Me.WindowState = FormWindowState.Maximized

            '=================
            restoreDatabase()
            LoadEmployee()
            '===================

            LoadAttendace()

            txtsalary.ReadOnly = True
            FormLogin.ActiveTimeWorkColorForAdmin()
        Catch ex As Exception

        End Try

    End Sub
    Public Sub LoadAttendace()
        DgvAttendace.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_ATTENDENT WHERE ACTIVE=1")
        Label12.Text = DgvAttendace.RowCount.ToString() & "   Record(s)"

        For Each row As DataGridViewRow In DgvAttendace.Rows
            If row.Cells("ABSENT_DATE").Value = Now.ToShortDateString Then

                row.Cells("ABSENT_DATE").Style.ForeColor = Color.Red


            End If

            'row.Cells("ABSENT_DATE").Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        Next
    End Sub

    Private Sub LBL_Report_Click(sender As Object, e As EventArgs) Handles LBL_Report.Click

        Me.TabMain.SelectedTab = TabReport

        EmployeeReport()
    End Sub
    Private Sub LBL_Attendance_Click(sender As Object, e As EventArgs) Handles LBL_Attendance.Click
        Me.TabMain.SelectedTab = TabAttendance
        LoadAttendace()
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            LBL_TIME.Text = Now.ToLongTimeString
            Timer1.Enabled = True
            Timer1.Interval = 1000


            If txtsysoff.Text = LBL_TIME.Text Then
                FormLogin.autoAttendace()
                Me.Close()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Public Sub validsymbal()
        vcode.ForeColor = Color.Red
        vname.ForeColor = Color.Red
        vsex.ForeColor = Color.Red

        vphone.ForeColor = Color.Red
        vdob.ForeColor = Color.Red
        vhiredate.ForeColor = Color.Red
        vposition.ForeColor = Color.Red
        vsalary.ForeColor = Color.Red
        vaddress.ForeColor = Color.Red
        vdd.ForeColor = Color.Red
    End Sub

    Private Sub LBL_Search_MouseHover(sender As Object, e As EventArgs) Handles LBL_Search.MouseHover

        LBL_Search.BackColor = Color.Black
    End Sub

    Private Sub LBL_Search_MouseLeave(sender As Object, e As EventArgs) Handles LBL_Search.MouseLeave

        If RanMumber = 1 Then
            LBL_Search.BackColor = Color.Black
        Else
            LBL_Search.BackColor = Color.Transparent
        End If


        '


    End Sub


    Private Sub LBL_Employee_MouseHover(sender As Object, e As EventArgs) Handles LBL_Employee.MouseHover
        LBL_Employee.BackColor = Color.Black
    End Sub

    Private Sub LBL_Employee_MouseLeave(sender As Object, e As EventArgs) Handles LBL_Employee.MouseLeave

        If TabMain.SelectedTab Is TabEmployee Then

            LBL_Employee.BackColor = Color.Black
        Else
            LBL_Employee.BackColor = Color.Transparent
        End If

    End Sub

    Private Sub LBL_Attendance_MouseHover(sender As Object, e As EventArgs) Handles LBL_Attendance.MouseHover
        LBL_Attendance.BackColor = Color.Black
    End Sub

    Private Sub LBL_Attendance_MouseLeave(sender As Object, e As EventArgs) Handles LBL_Attendance.MouseLeave
        If TabMain.SelectedTab Is TabAttendance Then
            LBL_Attendance.BackColor = Color.Black
        Else
            LBL_Attendance.BackColor = Color.Transparent
        End If

    End Sub

    Private Sub lBL_Holiday_Click(sender As Object, e As EventArgs) Handles lBL_Holiday.Click
        Me.TabMain.SelectedTab = TabHolidays

        loadholiday()
    End Sub

    Private Sub lBL_Holiday_MouseHover(sender As Object, e As EventArgs) Handles lBL_Holiday.MouseHover
        lBL_Holiday.BackColor = Color.Black
    End Sub

    Private Sub lBL_Holiday_MouseLeave(sender As Object, e As EventArgs) Handles lBL_Holiday.MouseLeave

        If TabMain.SelectedTab Is TabHolidays Then

            lBL_Holiday.BackColor = Color.Black
        Else
            lBL_Holiday.BackColor = Color.Transparent
        End If

    End Sub

    Private Sub LBL_Report_MouseHover(sender As Object, e As EventArgs) Handles LBL_Report.MouseHover
        LBL_Report.BackColor = Color.Black
    End Sub

    Private Sub LBL_Report_MouseLeave(sender As Object, e As EventArgs) Handles LBL_Report.MouseLeave

        If TabMain.SelectedTab Is TabReport Then

            LBL_Report.BackColor = Color.Black
        Else
            LBL_Report.BackColor = Color.Transparent
        End If
    End Sub

    Private Sub LBL_Permission_Click(sender As Object, e As EventArgs) Handles LBL_Permission.Click
        Me.TabMain.SelectedTab = TabPermission

        LoadUserLogin()
    End Sub
    Public Sub LoadUserLogin()
        DgvUserLogin.DataSource = Cmd.ExecuteDataTable("SELECT * FROM SYS_USER")
        LBL_TotalPermission.Text = DgvUserLogin.RowCount.ToString() & " Record(s)"

    End Sub

    Private Sub LBL_Permission_MouseHover(sender As Object, e As EventArgs) Handles LBL_Permission.MouseHover
        LBL_Permission.BackColor = Color.Black

    End Sub

    Private Sub LBL_Permission_MouseLeave(sender As Object, e As EventArgs) Handles LBL_Permission.MouseLeave


        If TabMain.SelectedTab Is TabPermission Then

            LBL_Permission.BackColor = Color.Black
        Else
            LBL_Permission.BackColor = Color.Transparent
        End If

    End Sub

    Private Sub LBL_UserRequest_Click(sender As Object, e As EventArgs) Handles LBL_UserRequest.Click
        Me.TabMain.SelectedTab = TabUserRequest


    End Sub

    Private Sub LBL_UserRequest_MouseHover(sender As Object, e As EventArgs) Handles LBL_UserRequest.MouseHover
        LBL_UserRequest.BackColor = Color.Black

    End Sub

    Private Sub LBL_UserRequest_MouseLeave(sender As Object, e As EventArgs) Handles LBL_UserRequest.MouseLeave


        If TabMain.SelectedTab Is TabUserRequest Then

            LBL_UserRequest.BackColor = Color.Black
        Else
            LBL_UserRequest.BackColor = Color.Transparent
        End If


    End Sub

    Private Sub LBL_Setting_Click(sender As Object, e As EventArgs) Handles LBL_Setting.Click
        Me.TabMain.SelectedTab = TabSetting

        Systemtime()
    End Sub

    Private Sub LBL_Setting_MouseHover(sender As Object, e As EventArgs) Handles LBL_Setting.MouseHover
        LBL_Setting.BackColor = Color.Black
    End Sub

    Private Sub LBL_Setting_MouseLeave(sender As Object, e As EventArgs) Handles LBL_Setting.MouseLeave
        'LBL_Setting.BackColor = Color.Transparent

        If TabMain.SelectedTab Is TabSetting Then

            LBL_Setting.BackColor = Color.Black
        Else
            LBL_Setting.BackColor = Color.Transparent
        End If

    End Sub

    Private Sub LBL_SystemRequire_Click(sender As Object, e As EventArgs) Handles LBL_RestoreDatabase.Click
        Me.TabMain.SelectedTab = TabSystemRequire


    End Sub

    Private Sub LBL_SystemRequire_MouseHover(sender As Object, e As EventArgs) Handles LBL_RestoreDatabase.MouseHover
        LBL_RestoreDatabase.BackColor = Color.Black
    End Sub

    Private Sub LBL_SystemRequire_MouseLeave(sender As Object, e As EventArgs) Handles LBL_RestoreDatabase.MouseLeave

        If TabMain.SelectedTab Is TabSystemRequire Then

            LBL_RestoreDatabase.BackColor = Color.Black
        Else
            LBL_RestoreDatabase.BackColor = Color.Transparent
        End If
    End Sub

    Private Sub LBL_About_Click(sender As Object, e As EventArgs) Handles LBL_About.Click

        RanMumber = 1

        LBL_Employee.BackColor = Color.Transparent
        LBL_Attendance.BackColor = Color.Transparent
        lBL_Holiday.BackColor = Color.Transparent
        LBL_Report.BackColor = Color.Transparent
        LBL_Permission.BackColor = Color.Transparent
        LBL_UserRequest.BackColor = Color.Transparent
        LBL_Setting.BackColor = Color.Transparent
        LBL_RestoreDatabase.BackColor = Color.Transparent
        LBL_LogOut.BackColor = Color.Transparent
        LBL_Search.BackColor = Color.Transparent

        Dim frm As New FormAbout
        frm.ShowDialog()
        If RanMumber = 1 Then
            LBL_About.BackColor = Color.Black

        Else
            LBL_About.BackColor = Color.Transparent
        End If

    End Sub

    Private Sub LBL_About_MouseHover(sender As Object, e As EventArgs) Handles LBL_About.MouseHover
        LBL_About.BackColor = Color.Black
    End Sub

    Private Sub LBL_About_MouseLeave(sender As Object, e As EventArgs) Handles LBL_About.MouseLeave


        If RanMumber = 1 Then
            LBL_About.BackColor = Color.Black
        Else
            LBL_About.BackColor = Color.Transparent
        End If
    End Sub

    Private Sub LBL_LogOut_Click(sender As Object, e As EventArgs) Handles LBL_LogOut.Click

        Me.PicProfile.Image = Nothing

        Me.TabMain.SelectedTab = TabHome
        Dim frm As New FormLogin
        frm.ShowDialog()



    End Sub

    Private Sub LBL_LogOut_MouseHover(sender As Object, e As EventArgs) Handles LBL_LogOut.MouseHover
        LBL_LogOut.BackColor = Color.Black
    End Sub

    Private Sub LBL_LogOut_MouseLeave(sender As Object, e As EventArgs) Handles LBL_LogOut.MouseLeave
        LBL_LogOut.BackColor = Color.Transparent

        If TabMain.SelectedTab Is TabLogOut Then

            LBL_LogOut.BackColor = Color.Black
        Else
            LBL_LogOut.BackColor = Color.Transparent
        End If
    End Sub



    Private Sub TabControl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabMain.SelectedTabChanged

        Try

            MainTab()
            '==============================================

            If TabMain.SelectedTab Is TabEmployee Then
                LBL_Employee.BackColor = Color.Black
                LBL_Attendance.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                LBL_Search.BackColor = Color.Transparent
                LBL_Employee_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabAttendance Then
                LBL_Attendance.BackColor = Color.Black
                LBL_Employee.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                LBL_Attendance_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabReport Then

                LBL_Report.BackColor = Color.Black
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                LBL_Report_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabHolidays Then
                lBL_Holiday.BackColor = Color.Black
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                lBL_Holiday_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabPermission Then

                LBL_Permission.BackColor = Color.Black
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                LBL_Permission_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabUserRequest Then
                LBL_UserRequest.BackColor = Color.Black
                LBL_Permission.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                LBL_UserRequest_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabUserRequest Then
                LBL_Setting.BackColor = Color.Black
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Logo.BackColor = Color.Transparent
                LBL_UserRequest_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabSystemRequire Then
                LBL_RestoreDatabase.BackColor = Color.Black
                LBL_Setting.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_LogOut.BackColor = Color.Transparent
                LBL_SystemRequire_Click(sender, e)
            ElseIf TabMain.SelectedTab Is TabLogOut Then
                LBL_LogOut.BackColor = Color.Black
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_Setting.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_LogOut_Click(sender, e)

            ElseIf TabMain.SelectedTab Is TabSetting Then
                LBL_Setting.BackColor = Color.Black
                LBL_RestoreDatabase.BackColor = Color.Transparent
                LBL_UserRequest.BackColor = Color.Transparent
                LBL_Permission.BackColor = Color.Transparent
                lBL_Holiday.BackColor = Color.Transparent
                LBL_Report.BackColor = Color.Transparent
                LBL_Attendance.BackColor = Color.Transparent
                LBL_Employee.BackColor = Color.Transparent
                LBL_Setting_Click(sender, e)

            End If

        Catch ex As Exception

        End Try
    End Sub




    Private Sub LBL_Employee_Click(sender As Object, e As EventArgs) Handles LBL_Employee.Click
        Me.TabMain.SelectedTab = TabEmployee

    End Sub


    Public Sub ColorTransparent()
        LBL_RestoreDatabase.BackColor = Color.Transparent

        LBL_Setting.BackColor = Color.Transparent
        LBL_UserRequest.BackColor = Color.Transparent
        LBL_Permission.BackColor = Color.Transparent
        lBL_Holiday.BackColor = Color.Transparent
        LBL_Report.BackColor = Color.Transparent
        LBL_Attendance.BackColor = Color.Transparent
        LBL_Employee.BackColor = Color.Transparent
        LBL_Logo.BackColor = Color.Transparent

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)


    End Sub
    Public Sub NewInsertAfternoon()
        Cmd.ExecuteNonquery("INSERT INTO TBL_TIME_WORK ([EMP_CODE],[EMP_NAME],[DATE],[MORNING_IN],[MORNING_OUT],[TOTAL],[AFTERNOON_IN]) VALUES ( '" & ecode & "','" & LBL_NAME.Text & "','" & Now.ToShortDateString & "','" & "Absent" & "','" & "Absent" & "','" & DBNull.Value & "','" & Now.ToLongTimeString & "')")
    End Sub

    Public Sub AbsentMorning() 'use for afternoon
        Try


            Dim MorOut = Cmd.ExecuteDataTable("SELECT  * FROM TBL_TIME_WORK WHERE EMP_CODE='" & ecode & "' and [DATE]='" & Now.ToShortDateString & "' ORDER BY [DATE]  DESC ")

            'ពេលព្រឹកនឹងម៉ោងចេញព្រឹកមានទិន្ន័យ(ស្មើ Abcent ទំាងពីរ)
            If MorOut.Rows.Count >= 1 And Convert.ToString(MorOut.Rows(0)("MORNING_IN")) <> "" And Convert.ToString(MorOut.Rows(0)("MORNING_OUT")) <> "" Then
                'MsgBox("1")

                Exit Sub
            End If
            'ពេលព្រឹកមានទិន្ន័យតែពេលចេញព្រឹកអត់មានទិន្ន័យ
            If MorOut.Rows.Count >= 1 And Convert.ToString(MorOut.Rows(0)("MORNING_IN")) <> "" Then

                Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [MORNING_OUT]= '" & FormLogin.mout & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Now.ToShortDateString & "'")
                'MsgBox("2")
                TotalMoring()
                Exit Sub
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub loadData()
        Try
            DataGridLoadData.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE EMP_CODE='" & ecode & "'ORDER BY [DATE] DESC ")

        Catch ex As Exception

        End Try

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)

    End Sub
    Public Sub LoginAfternoon()
        Try

            Dim DOut = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE [EMP_CODE]='" & ecode & "'ORDER BY [DATE] DESC ")
            'ធ្វើការបញ្ញូលទិន្ន័យទៅវេនថ្ងៃប្រសិនបើពេលព្រឹកមិនបានធ្វើការបញ្ចូល(សម្រាប់លើកដំបូងដែលអត់មានឈ្មោះបុគ្គលិកនៅក្នុងដេតាបេស)
            If DOut.Rows.Count = 0 Then

                NewInsertAfternoon()
                AbsentMorning()
                loadData()
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + "  you have been successful checked in at " + Now.ToLongTimeString)
                frm.ShowDialog()

                Exit Sub
            Else

            End If
            '======================================================
            'ធ្វើការបញ្ចូលទិន្ន័សម្រាប់ថ្ងៃស្អែក ឬ​ ថ្ងៃថ្មី
            If (DOut.Rows.Count >= 1) And DOut.Rows(0)("DATE") < Now.ToShortDateString Then

                NewInsertAfternoon()
                AbsentMorning()
                loadData()
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + "  you have been successful checked in at " + Now.ToLongTimeString)
                frm.ShowDialog()

                Exit Sub

            End If
            '=======================================================
            'ធ្វើការបញ្ញូលទិន្ន័យទៅវេនថ្ងៃប្រសិនបើពេលព្រឹកបានធ្វើការបញ្ចូល
            If DOut.Rows.Count >= 1 And Convert.ToString(DOut.Rows(0)("AFTERNOON_IN")) = "" Then


                Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [AFTERNOON_IN]= '" & Now.ToLongTimeString & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Now.ToShortDateString & "'")
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + "  you have been successful checked in at " + Now.ToLongTimeString)
                frm.ShowDialog()

                AbsentMorning()

                loadData()

                Exit Sub
                '==================================================================
                'មិនអោយធ្វើការបញ្ញូលទិន្ន័យទៅវេនថ្ងៃប្រសិនបើពេលថ្ងៃបានធ្វើការបញ្ចូលរួចម្តងហើយ
                '  MA.Hide()
            Else : moraf = DOut.Rows(0)("AFTERNOON_IN")
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Sorry ," + LBL_NAME.Text + "  you have already checked in at " + moraf)
                frm.ShowDialog()


                'MsgBox(LBL_NAME.Text + "  អ្នកបានបញ្ចូលទិន្ន័យម្តងរួចហើយនៅម៉ោង​​​​   " + moraf)


                AbsentMorning()
                loadData()

            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
    Public Sub LogOutAfternoon()
        Try


            'មិនអោយធ្វើការចាកចេញពីប្រពន្ធ័ប្រសិនបើគ្មានការបញ្ជូលទិន្ន័យទៅក្នុងប្រព័ន្ធជាមុន(គ្មានទិទ្ន័យទំាងពីរវេន គ្មានទិន្ន័យទំាងអស់នៅក្នុង ដេតាបេស)
            Dim DOut = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE [EMP_CODE]='" & ecode & "'ORDER BY [DATE] DESC ")

            If DOut.Rows.Count = 0 Then

                CannotLogOut()
                Exit Sub
            Else

            End If
            '=========================================================================================
            ''មិនអោយធ្វើការចាកចេញប្រសិនមិនមានការបញ្ចូលទិន្ន័យទៅក្នុងដេតាបេសសម្រាប់ថ្ងៃស្អែក ឬ ថ្ងៃថ្មិ
            If (DOut.Rows.Count >= 1) And DOut.Rows(0)("DATE") < Now.ToShortDateString Then

                CannotLogOut()

                Exit Sub
            Else
            End If
            '=======================================================================================
            'មិនអោយធ្វើការចាកចេញពីប្រពន្ធ័ប្រសិនបើគ្មានការបញ្ជូលទិន្ន័យទៅក្នុងប្រព័ន្ធជាមុន(គ្មានទិទ្ន័យវេនចូលថ្ងៃ)
            If DOut.Rows.Count >= 1 And Convert.ToString(DOut.Rows(0)("AFTERNOON_IN")) = "" Then

                CannotLogOut()

                Exit Sub
            Else

            End If
            '==========================================================================================
            'អនុញ្ញាតអោយចាកចេញព្រោះមានទិន្ន័យនៅក្នុងដេតាបេស
            If DOut.Rows.Count >= 1 And Convert.ToString(DOut.Rows(0)("AFTERNOON_OUT")) = "" Then


                Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [AFTERNOON_OUT]= '" & Now.ToLongTimeString & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Now.ToShortDateString & "'")
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + "  you have been successful checked out at " + Now.ToLongTimeString)
                frm.ShowDialog()

                TotalTimeAfternoon()
                loadData()

                Exit Sub
                '=======================================================================================
                'មិនអនុញ្ញាតអោយចាកចេញពីប្រពន្ធ័ព្រោះបានចាកចេញម្តងរួចហើយ
            Else : moraf = DOut.Rows(0)("AFTERNOON_OUT")

                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Sorry ," + LBL_NAME.Text + "  you have already checked out at " + moraf)
                frm.ShowDialog()


                loadData()

            End If
        Catch ex As Exception

        End Try
    End Sub
    Public Sub LoginMorning()

        Try


            '=========================================================================================
            'ធ្វើការបញ្ចូលទិន្ន័យលើកដំបូងប្រសិនបើគ្មានទិន្ន័នៅក្នុងដេតាបេស
            Dim dtt = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE EMP_CODE='" & ecode & "'ORDER BY [DATE] DESC ")
            If (dtt.Rows.Count = 0) Then
                Cmd.ExecuteNonquery("INSERT INTO TBL_TIME_WORK ([EMP_CODE],[EMP_NAME],[DATE],[MORNING_IN]) VALUES ( '" & ecode & "','" & LBL_NAME.Text & "','" & Now.ToShortDateString & "','" & Now.ToLongTimeString & "')")
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + " you have been successful checked in at " + Now.ToLongTimeString)
                frm.ShowDialog()

                AbsentAfterNoon()
                loadData()
                Exit Sub
            Else
            End If

            '======================================================================================
            'ធ្វើការបញ្ជុលទិន្ន័យសម្រាប់ថ្ងៃស្អែក ឬ ថ្ងៃថ្មី


            If (dtt.Rows.Count >= 1) And dtt.Rows(0)("Date") < Now.ToShortDateString Then

                Cmd.ExecuteNonquery("INSERT INTO TBL_TIME_WORK ([EMP_CODE],[EMP_NAME],[DATE],[MORNING_IN]) VALUES ( '" & ecode & "','" & LBL_NAME.Text & "','" & Now.ToShortDateString & "','" & Now.ToLongTimeString & "')")

                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + " you have been successful checked in at " + Now.ToLongTimeString)
                frm.ShowDialog()

                AbsentAfterNoon()
                TotalTimeAfternoonOut()
                loadData()



            Else
                '===============================================================================
                'មិនអាចបញ្ចុលទិន្ន័យបានប្រសិនបើបានចូលហើយម្តងនោះ
                moraf = dtt.Rows(0)("MORNING_IN")
                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Sorry ," + LBL_NAME.Text + " you have already checked in at " + moraf)
                frm.ShowDialog()

                AbsentAfterNoon()
                loadData()


            End If

        Catch ex As Exception

        End Try
    End Sub
    Public Sub LogOutMorning()
        Try


            '======================================================================================
            'ការចាកចេញមិនបានសម្រេចប្រសិនបើមិនបានបញ្ចូលទិន្ន័យជាមុន(ពេលព្រឹកថ្ងៃនេះ)
            Dim MorOut = Cmd.ExecuteDataTable("SELECT  * FROM TBL_TIME_WORK WHERE EMP_CODE='" & ecode & "' ORDER BY [DATE]  DESC ")
            If MorOut.Rows.Count = 0 Then
                CannotLogOut()

                Exit Sub
            Else
            End If
            '======================================================================================
            'មិនអោយធ្វើការចាកចេញប្រសិនមិនមានការបញ្ចូលទិន្ន័យទៅក្នុងដេតាបេសសម្រាប់ថ្ងៃស្អែក ឬ ថ្ងៃថ្មិ
            If (MorOut.Rows.Count >= 1) And MorOut.Rows(0)("DATE") < Now.ToShortDateString Then

                CannotLogOut()

                Exit Sub
            Else
            End If
            '=======================================================================================
            'អនុញ្ញាតអោយចាកចេញពីប្រពន្ធ័

            If MorOut.Rows.Count >= 1 And Convert.ToString(MorOut.Rows(0)("MORNING_OUT")) = "" Then

                Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [MORNING_OUT]= '" & Now.ToLongTimeString & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Now.ToShortDateString & "'")

                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Thank ," + LBL_NAME.Text + " you have been sucessful checked out at " + Now.ToLongTimeString)
                frm.ShowDialog()

                TotalMoring()
                loadData()

                Exit Sub
                '======================================================================================

                'មិនអនុញ្ញាតអោយចាកចេញពីប្រពន្ធ័ព្រោះបានចាកចេញម្តងរួចហើយ


            Else : moraf = MorOut.Rows(0)("MORNING_OUT")

                Dim frm As New FormMessage
                frm.LBL_Message.Text = ("Sorry ," + LBL_NAME.Text + " you have already checked out at " + moraf)
                frm.ShowDialog()

                loadData()

            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Public Sub CannotLogOut()
        Try
            FormViewTime.Label1.ForeColor = Color.Red
            Dim frm As New FormMessage
            frm.LBL_Message.Text = ("Sorry ," + LBL_NAME.Text + "  checked out is unsucessful please try again to check in first")
            frm.ShowDialog()

            loadData()
        Catch ex As Exception

        End Try

    End Sub
    Public Sub TotalTimeAfternoon()
        Try


            Dim timePeriod As TimeSpan
            Dim Total = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE [EMP_CODE]='" & ecode & "'ORDER BY [DATE] DESC ")
            timePeriod = CDate(Total.Rows(0)("AFTERNOON_OUT")) - CDate(Total.Rows(0)("AFTERNOON_IN"))
            Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [ATOTAL]= '" & timePeriod.ToString() & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Now.ToShortDateString & "'")
        Catch ex As Exception

        End Try
    End Sub
    Public Sub TotalTimeAfternoonOut()
        Try


            Dim timePeriod As TimeSpan
            Dim Total = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE [EMP_CODE]='" & ecode & "' and [DATE] ='" & Today.AddDays(-1) & "'")

            timePeriod = CDate(Total.Rows(0)("AFTERNOON_OUT")) - CDate(Total.Rows(0)("AFTERNOON_IN"))
            Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [ATOTAL]= '" & timePeriod.ToString() & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Today.AddDays(-1) & "'")
        Catch ex As Exception

        End Try
    End Sub
    Public Sub TotalMoring()
        Try

            Dim timePeriod As TimeSpan
            Dim Total = Cmd.ExecuteDataTable("SELECT * FROM TBL_TIME_WORK WHERE [EMP_CODE]='" & ecode & "'ORDER BY [DATE] DESC ")
            timePeriod = CDate(Total.Rows(0)("MORNING_OUT")) - CDate(Total.Rows(0)("MORNING_IN"))
            Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [TOTAL]= '" & timePeriod.ToString() & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Now.ToShortDateString & "'")

        Catch ex As Exception

        End Try
    End Sub

    Public Sub AbsentAfterNoon() 'use for Morning
        ' On Error Resume Next
        Try


            Dim MorOut = Cmd.ExecuteDataTable("SELECT  * FROM TBL_TIME_WORK WHERE EMP_CODE='" & ecode & "' and [DATE]='" & Today.AddDays(-1) & "' ORDER BY [DATE]  DESC ")

            'សម្រាប់ថ្ងៃបច្ចុប្បន្ន​ប្រសិនបើពេលចូលថ្ងៃ និង​ ពេលចេញថ្ងៃមានទិន្ន័យ
            If MorOut.Rows.Count >= 1 And Convert.ToString(MorOut.Rows(0)("AFTERNOON_IN")) <> "" And Convert.ToString(MorOut.Rows(0)("AFTERNOON_OUT")) <> "" Then
                'MsgBox("1")

                Exit Sub
            End If
            'សម្រាប់ថ្ងៃថ្មីប្រសិនបើពេលចេញថ្ងៃគ្មានទិន្ន័យ
            If MorOut.Rows.Count >= 1 And Convert.ToString(MorOut.Rows(0)("AFTERNOON_IN")) <> "" Then

                Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [AFTERNOON_OUT]= '" & FormLogin.afout & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Today.AddDays(-1) & "'")
                'MsgBox("2")
                TotalTimeAfternoon()

                Exit Sub
            End If
            'សម្រាប់ថ្ងៃថ្មីប្រសិនបើពេលចូលថ្ងៃ និង ពេលចេញថ្ងៃគ្មានទិន្ន័យ
            If MorOut.Rows.Count >= 1 And Convert.ToString(MorOut.Rows(0)("AFTERNOON_IN")) = "" Or Convert.ToString(MorOut.Rows(0)("AFTERNOON_OUT")) = "" Then
                Cmd.ExecuteNonquery("UPDATE [TBL_TIME_WORK] SET [MORNING_OUT]='" & "Absent" & "',[AFTERNOON_IN]= '" & "Absent" & "',[AFTERNOON_OUT]='" & "Absent" & "' WHERE [EMP_CODE]='" & ecode & "' and [DATE]= '" & Today.AddDays(-1) & "'")

                'MsgBox("3")


            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cbodegree_Leave(sender As Object, e As EventArgs) Handles cbodegree.Leave
        If cbodegree.Text <> "" Then

            Me.Highlighter1.SetHighlightColor(Me.cbodegree, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(cbodegree, "")
        End If
    End Sub

    Public Sub cbodegree_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbodegree.SelectedIndexChanged

        Try
            degreeid = Cmd.ExecuteScalar("SELECT  DEGREE_ID FROM TBL_DEGREE WHERE DEGREENAME ='" + cbodegree.SelectedValue + "'")

        Catch ex As Exception

        End Try

    End Sub

    Private Sub CboPostion_Leave(sender As Object, e As EventArgs) Handles CboPostion.Leave
        If CboPostion.Text <> "" Then


            Me.Highlighter1.SetHighlightColor(Me.CboPostion, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(CboPostion, "")
        End If
    End Sub
    Public Sub CboPostion_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboPostion.SelectedIndexChanged

        Try

            txtsalary.Text = Cmd.ExecuteScalar("SELECT  SALARY FROM TBL_POSITION WHERE POSITION_NAME ='" + CboPostion.SelectedValue + "'")

            positionid = Cmd.ExecuteScalar("SELECT  POSITION_ID FROM TBL_POSITION WHERE POSITION_NAME ='" + CboPostion.SelectedValue + "'")

        Catch ex As Exception

        End Try
    End Sub

    Private Sub cbosex_Leave(sender As Object, e As EventArgs) Handles cbosex.Leave
        If cbosex.Text = "" Then
            ValidSex()

            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.cbosex, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(cbosex, "")

        End If
    End Sub


    Private Sub cbosex_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbosex.SelectedIndexChanged

        Try
            sexid = Cmd.ExecuteScalar("SELECT  SEX_ID FROM TBL_SEX WHERE NAME ='" + cbosex.SelectedValue + "'")

        Catch ex As Exception

        End Try
        'MsgBox(sexid.ToString)

    End Sub

    Private Sub CboAddress_Leave(sender As Object, e As EventArgs) Handles CboAddress.Leave
        If CboAddress.Text <> "" Then

            Me.Highlighter1.SetHighlightColor(Me.CboAddress, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(CboAddress, "")
        End If
    End Sub
    Private Sub CboAddress_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboAddress.SelectedIndexChanged
        Try
            addressid = Cmd.ExecuteScalar("SELECT ADDRESS_ID FROM TBL_ADDRESS WHERE ADDRESS_NAME='" & CboAddress.SelectedValue & "'")

        Catch ex As Exception

        End Try
    End Sub
    Public Sub ValidEmployeeCode()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.txtcode, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        'tooltip.AutoPopDelay = 100
        tooltip.InitialDelay = 10
        '  tooltip.ReshowDelay = 500
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(txtcode, "Please input employee code.")
        txtcode.SelectAll()
        txtcode.Focus()
    End Sub
    Public Sub ValidEmployeeName()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.txtname, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(txtname, "Please input employee Name.")
        txtname.SelectAll()
        txtname.Focus()
    End Sub
    Public Sub ValidSex()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.cbosex, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(cbosex, "Please input sex.")
        cbosex.Focus()
    End Sub
    Public Sub ValidEmployeeLenght()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.txtname, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Error employee name."
        tooltip.ToolTipIcon = ToolTipIcon.Error
        tooltip.SetToolTip(txtname, "Employee name must be between 2 to 20 charactors lenght.")
        txtname.SelectAll()
        txtname.Focus()
    End Sub
    Public Sub ValidDegree()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.cbodegree, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(cbodegree, "Please input degree.")
        cbodegree.SelectAll()
        cbodegree.Focus()
    End Sub
    Public Sub ValidPhoneNumber()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.txtphone, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(txtphone, "Please input phone number.")

        txtphone.Focus()
    End Sub
    Public Sub ValidPosition()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.CboPostion, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(CboPostion, "Please input position.")
        CboPostion.Focus()
    End Sub
    Public Sub ValidAddress()
        tooltip.IsBalloon = True
        Me.Highlighter1.SetHighlightColor(Me.CboAddress, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
        tooltip.InitialDelay = 10
        tooltip.ToolTipTitle = "Warning"
        tooltip.ToolTipIcon = ToolTipIcon.Warning
        tooltip.SetToolTip(CboAddress, "Please input address!")
        CboAddress.Focus()
    End Sub
    Private Sub btn_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Add.Click
        randomPasswords()
        Try



            If lblID.Text = "" Then
                If txtcode.Text = "" Then
                    ValidEmployeeCode()

                    Exit Sub
                Else
                    tooltip.SetToolTip(txtcode, "")
                End If
                '=========================================
                If txtname.Text = "" Then

                    ValidEmployeeName()
                    Exit Sub
                Else
                    tooltip.SetToolTip(txtname, "")
                End If
                '============================================

                If cbosex.Text = "" Then
                    ValidSex()

                    Exit Sub
                Else
                    tooltip.SetToolTip(cbosex, "")

                End If
                '===============================================
                If Me.txtname.Text.Length < 2 Or Me.txtname.Text.Length > 20 Then

                    ValidEmployeeLenght()

                    Exit Sub
                Else
                    tooltip.SetToolTip(txtname, "")
                End If
                '==================================================
                If cbodegree.Text = "" Then
                    ValidDegree()

                    Exit Sub
                Else
                    tooltip.SetToolTip(cbodegree, "")
                End If
                '==========================================
                If Me.txtphone.Text.Trim() = "" Then
                    ValidPhoneNumber()

                    Exit Sub
                Else
                    tooltip.SetToolTip(txtphone, "")
                End If
                '===============================================
                If CboPostion.Text = "" Then
                    ValidPosition()

                    Exit Sub
                Else
                    tooltip.SetToolTip(CboPostion, "")
                End If
                '=================================================
                If CboAddress.Text = "" Then
                    ValidAddress()

                    Exit Sub
                Else
                    tooltip.SetToolTip(CboAddress, "")
                End If

                'Valid DeuplicateDate

                '''''''''''''''''''''''''''''''''''''''''
                Dim countempid As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM TBL_EMPLOYEE WHERE EMP_CODE ='" & txtcode.Text & "' ")
                If countempid > 0 Then
                    tooltip.IsBalloon = True
                    tooltip.InitialDelay = 10
                    tooltip.ToolTipTitle = "Duplicate with " + txtcode.Text + ""
                    tooltip.ToolTipIcon = ToolTipIcon.Error
                    tooltip.SetToolTip(txtcode, "Employee code is already existed.")
                    txtcode.SelectAll()
                    txtcode.Focus()
                    Exit Sub
                Else
                    tooltip.SetToolTip(txtcode, "")
                End If

                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                Dim count As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM TBL_EMPLOYEE WHERE EMP_NAME ='" & txtname.Text & "'")
                If count > 0 Then
                    tooltip.IsBalloon = True
                    tooltip.InitialDelay = 10
                    tooltip.ToolTipTitle = "Duplicate with " + txtname.Text + ""
                    tooltip.ToolTipIcon = ToolTipIcon.Error
                    tooltip.SetToolTip(txtname, "Employee name is already existed.")
                    txtname.SelectAll()
                    txtname.Focus()
                    Exit Sub
                Else
                    tooltip.SetToolTip(txtname, "")
                End If
                '=============================================
                If Validator.IsPhone(Me.txtphone.Text) = False Then

                    tooltip.IsBalloon = True
                    tooltip.InitialDelay = 10
                    tooltip.ToolTipTitle = "Error phone number."
                    tooltip.ToolTipIcon = ToolTipIcon.Error
                    tooltip.SetToolTip(txtphone, "Please input a correct phone number format.")
                    txtphone.SelectAll()
                    txtphone.Focus()
                    Exit Sub
                Else
                    tooltip.SetToolTip(txtphone, "")
                End If
                ''''''''''''''''''''''''''''''''''''''''''''''
                Dim countphone As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM TBL_EMPLOYEE WHERE PHONE ='" & txtphone.Text & "'")
                If countphone > 0 Then
                    tooltip.IsBalloon = True
                    tooltip.InitialDelay = 10
                    tooltip.ToolTipTitle = "Duplicate with " + txtphone.Text + ""
                    tooltip.ToolTipIcon = ToolTipIcon.Error
                    tooltip.SetToolTip(txtphone, "Phone number is already existed.")
                    txtphone.SelectAll()
                    txtphone.Focus()
                    Exit Sub
                Else
                    tooltip.SetToolTip(txtphone, "")
                End If
                '=================================================
                Try
                    'Picture location=nothing
                    If ImgLoc = "" Then
                        Cmd.ExecuteNonquery("INSERT INTO TBL_EMPLOYEE (EMP_CODE,EMP_NAME,SEX_ID,ADDRESS_ID,DEGREE_ID,PHONE,DOB,HIRE_DATE,POSITION_ID,SALARY,PHOTO,ACTIVE) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & sexid & "','" & addressid & "','" & degreeid & "','" & txtphone.Text & "','" & DTPDOB.Text & "','" & DTPHireDate.Text & "','" & positionid & "','" & txtsalary.Text & "',NULL,1)")
                        insertIntoUserLogin()
                        MsgBox("1 row records(s) saved")
                        storeEmployeeAttendance()

                        LoadEmployee()

                        ClearAllTextBox()

                        Exit Sub

                    End If
                    'Picture Location<>nothing
                    Dim img As Byte() = Nothing
                    Dim fs As New FileStream(ImgLoc, FileMode.Open, FileAccess.Read)
                    Dim br As New BinaryReader(fs)
                    img = br.ReadBytes(CInt(fs.Length))
                    Dim sql As String = ("INSERT INTO TBL_EMPLOYEE (EMP_CODE,EMP_NAME,SEX_ID,ADDRESS_ID,DEGREE_ID,PHONE,DOB,HIRE_DATE,POSITION_ID,SALARY,PHOTO,ACTIVE) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & sexid & "','" & addressid & "','" & degreeid & "','" & txtphone.Text & "','" & DTPDOB.Text & "','" & DTPHireDate.Text & "','" & positionid & "','" & txtsalary.Text & "',@img,1)")


                    command = New SqlCommand(sql, Cmd.GetConnection)
                    command.Parameters.Add(New SqlParameter("@img", img))
                    'Dim x As Integer = command.ExecuteNonQuery()
                    command.ExecuteNonQuery()
                    insertIntoUserLogin()


                    LoadEmployee()
                    PanelNopicture.Show()
                    ClearAllTextBox()
                    '   MessageBox.Show(x.ToString() & "Records(s) saved")

                    storeEmployeeAttendance()
                Catch ex As Exception
                    MsgBox(ex.Message)

                End Try
                '=================================================


            Else
                PanelNopicture.Show()
                ClearAllTextBox()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub ClearAllTextBox()

        txtcode.Text = ""
        txtname.Text = ""
        cbosex.Text = ""
        cbodegree.Text = ""
        txtphone.Text = ""
        cbodegree.Text = ""
        DTPDOB.Text = Now
        DTPHireDate.Text = Now
        txtsalary.Text = ""
        CboAddress.Text = ""
        CboPostion.Text = ""
        lblID.Text = ""
        txtcode.Focus()
        PicEmployee.Image = Nothing
    End Sub
    Public Sub LoadEmployee()
        Try


            DgLoadEmployee.DataSource = Cmd.ExecuteDataTable("SELECT *FROM TBL_EMPLOYEE E JOIN  TBL_DEGREE D ON D.DEGREE_ID=E.DEGREE_ID JOIN TBL_POSITION P ON P.POSITION_ID=E.POSITION_ID JOIN TBL_SEX S ON S.SEX_ID=E.SEX_ID JOIN TBL_ADDRESS A ON A.ADDRESS_ID=E.ADDRESS_ID WHERE E.ACTIVE=1")

            Label10.Text = DgLoadEmployee.RowCount.ToString() & "   Record(s)"
            loadImageEmplyeeNull()

        Catch ex As Exception

        End Try
    End Sub
    Public Sub restoreDatabase()
        Try


            DgvRestoreDb.DataSource = Cmd.ExecuteDataTable("SELECT *FROM TBL_EMPLOYEE E JOIN  TBL_DEGREE D ON D.DEGREE_ID=E.DEGREE_ID JOIN TBL_POSITION P ON P.POSITION_ID=E.POSITION_ID JOIN TBL_SEX S ON S.SEX_ID=E.SEX_ID JOIN TBL_ADDRESS A ON A.ADDRESS_ID=E.ADDRESS_ID WHERE E.ACTIVE=0")
            Label13.Text = DgvRestoreDb.RowCount.ToString() & "   Record(s)"
        Catch ex As Exception

        End Try
    End Sub
    Private Sub SplitContainer1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles SplitContainer1.MouseDoubleClick
        SplitContainer1.Panel1Collapsed = True
    End Sub

    Private Sub DgLoadEmployee_CurrentCellChanged(sender As Object, e As EventArgs) Handles DgLoadEmployee.CurrentCellChanged


        Try

            For Each row As DataGridViewRow In DgLoadEmployee.SelectedRows
                txtcode.Text = row.Cells("EMP_CODE1").Value

                txtname.Text = row.Cells("EMP_NAME1").Value
                cbosex.Text = row.Cells("SEX_NAME").Value
                cbodegree.Text = row.Cells("DEGREENAME").Value
                txtphone.Text = row.Cells("PHONE").Value
                DTPDOB.Text = row.Cells("DOB").Value
                DTPHireDate.Text = row.Cells("HIRE_DATE").Value
                CboPostion.Text = row.Cells("POSITION_NAME").Value
                txtsalary.Text = row.Cells("SALARY").Value
                CboAddress.Text = row.Cells("ADDRESS_NAME").Value

                lblID.Text = row.Cells("ID").Value
                loadImageEmplyee()

            Next
        Catch ex As Exception

        End Try

    End Sub


    Private Sub FullScreenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FullScreenToolStripMenuItem.Click

        SplitContainer1.Panel1Collapsed = True

    End Sub


    Private Sub DefultToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DefultToolStripMenuItem.Click
        SplitContainer1.Panel1Collapsed = False

    End Sub

    Private Sub btn_Edit_Click(sender As Object, e As EventArgs) Handles btn_Edit.Click

        Try

            If txtcode.Text = "" Then

                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(txtcode, "Please input employee code.")
                txtcode.SelectAll()
                txtcode.Focus()
                Exit Sub
            Else

                tooltip.SetToolTip(txtcode, "")
            End If
            If txtname.Text = "" Then

                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(txtname, "Please input employee Name.")
                txtname.SelectAll()
                txtname.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtname, "")

            End If
            If cbosex.Text = "" Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(cbosex, "Please input sex.")
                cbosex.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(cbosex, "")
            End If
            If Me.txtname.Text.Length < 2 Or Me.txtname.Text.Length > 20 Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Error employee name!"
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtname, "Employee name must be between 2 to 20 charactors lenght")
                txtname.SelectAll()
                txtname.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtname, "")
            End If
            If cbodegree.Text = "" Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(cbodegree, "Please input degree.")
                cbodegree.SelectAll()
                cbodegree.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(cbodegree, "")
            End If
            If Me.txtphone.Text.Trim() = "" Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(txtphone, "Please input phone number.")
                txtphone.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtphone, "")
            End If
            If CboPostion.Text = "" Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(CboPostion, "Please input position.")
                CboPostion.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(CboPostion, "")
            End If
            If CboAddress.Text = "" Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Warning"
                tooltip.ToolTipIcon = ToolTipIcon.Warning
                tooltip.SetToolTip(CboAddress, "Please input address.")
                CboAddress.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(CboAddress, "")
            End If
            ''''''''''''''''''''''''''''
            Dim countempid As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM TBL_EMPLOYEE WHERE EMP_CODE ='" & txtcode.Text & "' AND EMP_ID <>'" & lblID.Text & "'")
            If countempid > 0 Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Duplicate with " + txtcode.Text + ""
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtcode, "Employee code is already existed.")

                txtcode.SelectAll()
                txtcode.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtcode, "")
            End If
            Dim count As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM TBL_EMPLOYEE WHERE EMP_NAME ='" & txtname.Text & "' AND EMP_ID  <>'" & lblID.Text & "'")
            If count > 0 Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Duplicate with " + txtname.Text + ""
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtname, "Employee name is already existed.")
                txtname.SelectAll()
                txtname.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtname, "")
            End If
            '=============================================
            If Validator.IsPhone(Me.txtphone.Text) = False Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Error phone number."
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtphone, "Please input a correct phone number format.")
                txtphone.SelectAll()
                txtphone.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtphone, "")
            End If
            ''''''''''''''''''''''''''''''''''''''''''''''
            Dim countphone As Int16 = Cmd.ExecuteScalar("SELECT COUNT(*) FROM TBL_EMPLOYEE WHERE PHONE ='" & txtphone.Text & "'AND EMP_ID  <>'" & lblID.Text & "'")
            If countphone > 0 Then
                tooltip.IsBalloon = True
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Duplicate with " + txtphone.Text + ""
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtphone, "Phone number is already existed.")

                txtphone.SelectAll()
                txtphone.Focus()
                Exit Sub
            Else
                tooltip.SetToolTip(txtphone, "")
            End If
        Catch ex As Exception


        End Try
        '-------------------------------------------------------------
        Try
            If ImgLoc.ToString = "" Then
                Cmd.ExecuteNonquery("UPDATE TBL_EMPLOYEE SET EMP_CODE='" & txtcode.Text & "',EMP_NAME='" & txtname.Text & "',SEX_ID='" & sexid & "',DEGREE_ID='" & degreeid & "',PHONE='" & txtphone.Text & "', DOB='" & DTPDOB.Text & "',HIRE_DATE='" & DTPHireDate.Text & "',POSITION_ID='" & positionid & "',SALARY='" & txtsalary.Text & "',ADDRESS_ID='" & addressid & "' WHERE EMP_ID='" & lblID.Text & "'")
                updateUserLogin()
                updatetmpEmployee()
                LoadEmployee()
                loadImageEmplyee()
                Exit Sub

            End If
            Dim img As Byte() = Nothing
            Dim fs As New FileStream(ImgLoc, FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(fs)

            img = br.ReadBytes(CInt(fs.Length))
            Dim sql As String = ("UPDATE TBL_EMPLOYEE SET EMP_CODE='" & txtcode.Text & "',EMP_NAME='" & txtname.Text & "',SEX_ID='" & sexid & "',DEGREE_ID='" & degreeid & "',PHONE='" & txtphone.Text & "', DOB='" & DTPDOB.Text & "',HIRE_DATE='" & DTPHireDate.Text & "',POSITION_ID='" & positionid & "',SALARY='" & txtsalary.Text & "',ADDRESS_ID='" & addressid & "',PHOTO=@img WHERE EMP_ID='" & lblID.Text & "'")
            updateUserLogin()
            updatetmpEmployee()
            command = New SqlCommand(sql, Cmd.GetConnection)
            command.Parameters.Add(New SqlParameter("@img", img))
            'Dim x As Integer = command.ExecuteNonQuery()
            command.ExecuteNonQuery()
            ' MessageBox.Show(x.ToString() & "Records(s) updated")

            LoadEmployee()
            loadImageEmplyee()
        Catch ex As Exception

            MsgBox(ex.Message)
        End Try


    End Sub
    Public Sub loadsex()

        Try
            cbosex.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_SEX")
            cbosex.DisplayMember = "NAME"
            cbosex.ValueMember = "NAME"

        Catch ex As Exception

        End Try
    End Sub

    Public Sub LoadPosition()

        Try
            CboPostion.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_POSITION ORDER BY [POSITION_NAME]")

            CboPostion.DisplayMember = "POSITION_NAME"

            CboPostion.ValueMember = "POSITION_NAME"


        Catch ex As Exception


        End Try


    End Sub



    Public Sub loadDegree()
        Try

            cbodegree.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_DEGREE")
            cbodegree.DisplayMember = "DEGREENAME"
            cbodegree.ValueMember = "DEGREENAME"

        Catch ex As Exception

        End Try
    End Sub



    Public Sub loadAddress()
        Try
            CboAddress.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_ADDRESS ORDER BY [ADDRESS_NAME] ASC")
            CboAddress.DisplayMember = "ADDRESS_NAME"
            CboAddress.ValueMember = "ADDRESS_NAME"
        Catch ex As Exception

        End Try

    End Sub


    Private Sub btn_close_Click(sender As Object, e As EventArgs) Handles btn_close.Click
        Me.Close()
    End Sub

    Private Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        ClearAllTextBox()
    End Sub

    Private Sub btn_Delete_Click(sender As Object, e As EventArgs) Handles btn_Delete.Click

        Try


            For Each row As DataGridViewRow In DgLoadEmployee.SelectedRows
                Dim dr = row.Cells(0).Value
                Dim empcode = row.Cells(1).Value
                Cmd.ExecuteNonquery("UPDATE TBL_EMPLOYEE SET ACTIVE=0 WHERE EMP_ID='" & dr & "'")
                Cmd.ExecuteNonquery(" UPDATE TBL_STORE_EMPLOYEE SET ABSENT_DATE='DE' WHERE EMP_ID='" & dr & "'")
                Cmd.ExecuteNonquery(" UPDATE SYS_USER SET ACTIVE=0 WHERE USER_CODE='" & empcode & "'")

            Next
            PanelNopicture.Show()
            ClearAllTextBox()
            LoadEmployee()
            restoreDatabase()

        Catch ex As Exception

        End Try
    End Sub

    Private Sub RestoreToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreToolStripMenuItem.Click

        Try


            If DgvRestoreDb.RowCount = 0 Then
                FormMessage.LBL_Message.Text = "No database to restore!"
                FormMessage.Size = New System.Drawing.Size(340, 129)
                FormMessage.ShowDialog()

                Exit Sub
            End If
            For Each row As DataGridViewRow In DgvRestoreDb.SelectedRows
                Dim dr = row.Cells(0).Value

                'Dim empcode = row.Cells("EMP_CODE2").Value


                Cmd.ExecuteNonquery("UPDATE TBL_EMPLOYEE SET ACTIVE=1 WHERE EMP_ID='" & dr & "'")
                Cmd.ExecuteNonquery("UPDATE TBL_STORE_EMPLOYEE SET ABSENT_DATE='" & Now.ToShortDateString & "' WHERE ABSENT_DATE='DE' and EMP_ID='" & dr & "'")
                Cmd.ExecuteNonquery(" UPDATE SYS_USER SET ACTIVE=1 WHERE EMP_ID ='" & dr & "'")

            Next
            FormProgressLoad.ShowDialog()
            restoreDatabase()
            LoadEmployee()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btn_Refresh_Click(sender As Object, e As EventArgs) Handles btn_Refresh.Click
        FormProgressLoad.ShowDialog()
        LoadEmployee()
        ClearAllTextBox()
        PanelNopicture.Show()
    End Sub

    Private Sub RestoreAllToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreAllToolStripMenuItem.Click
        DgvRestoreDb.MultiSelect = True

        Try


            If DgvRestoreDb.RowCount = 0 Then
                FormMessage.LBL_Message.Text = "No database to restore!"
                FormMessage.Size = New System.Drawing.Size(340, 129)
                FormMessage.ShowDialog()
                Exit Sub
            End If
            DgvRestoreDb.SelectAll()
            For Each row As DataGridViewRow In DgvRestoreDb.SelectedRows
                Dim dr = row.Cells(0).Value
                Cmd.ExecuteNonquery("UPDATE TBL_EMPLOYEE SET ACTIVE=1 WHERE EMP_ID='" & dr & "'")
                Cmd.ExecuteNonquery("UPDATE TBL_STORE_EMPLOYEE SET ABSENT_DATE='" & Now.ToShortDateString & "' WHERE ABSENT_DATE='DE' and EMP_ID='" & dr & "'")
                Cmd.ExecuteNonquery(" UPDATE SYS_USER SET ACTIVE=1 WHERE EMP_ID ='" & dr & "'")

            Next
            FormProgressLoad.ShowDialog()
            restoreDatabase()
            LoadEmployee()
        Catch ex As Exception

        End Try
    End Sub
    Public Sub loadImageEmplyee()
        Dim pic = Cmd.ExecuteDataTable("SELECT  PHOTO FROM TBL_EMPLOYEE WHERE EMP_ID='" & lblID.Text & "'")

        If pic.Rows.Count > 0 And Convert.ToString(pic.Rows(0)("PHOTO")) = "" Then
            PicEmployee.Image = Nothing
            PanelNopicture.Show()
            Exit Sub
        Else
            PanelNopicture.Hide()
        End If

        '''''''''''''''''''
        Try
            Dim sql As String = "Select  PHOTO from TBL_EMPLOYEE where EMP_ID='" + lblID.Text + "'"

            Dim cmd1 As New SqlCommand(sql, Cmd.GetConnection)
            Dim reader As SqlDataReader = cmd1.ExecuteReader()
            reader.Read()
            If reader.HasRows Then

                Dim img As Byte() = DirectCast(reader(0), Byte())
                If img Is Nothing Then
                    PicEmployee.Image = Nothing
                Else
                    Dim ms As New MemoryStream(img)
                    PicEmployee.Image = Image.FromStream(ms)


                End If
            Else
                '   MessageBox.Show("This is does not exist. ")



            End If
        Catch ex As Exception


        End Try

    End Sub
    Public Sub loadImageEmplyeeNull()
        Try


            Dim pic = Cmd.ExecuteDataTable("SELECT  PHOTO FROM TBL_EMPLOYEE") ' WHERE EMP_CODE='" & ecode & "'")

            If pic.Rows.Count > 0 And Convert.ToString(pic.Rows(0)("PHOTO")) <> "" Then
                PicEmployee.Image = Nothing

            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Sub PicEmployee_Click(sender As Object, e As EventArgs) Handles PicEmployee.Click
        Try
            Dim FileDialog As New OpenFileDialog()
            FileDialog.Title = "Please select you picture"

            FileDialog.Filter = "image file(*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png "
            If FileDialog.ShowDialog(Me) = DialogResult.OK Then

                PicEmployee.Image = Image.FromFile(FileDialog.FileName)

                ImgLoc = FileDialog.FileName.ToString()
                PicEmployee.ImageLocation = FileDialog.FileName


            End If


        Catch ex As Exception


        End Try
    End Sub

    Private Sub btn_view_Click(sender As Object, e As EventArgs) Handles btn_view.Click
        'MsgBox(ImgLoc.ToString)
        MsgBox("This option is under contruction", vbInformation, "TimeManagementSystem")
    End Sub

    Private Sub LBL_NAME_Click(sender As Object, e As EventArgs) Handles LBL_NAME.Click
        DgLoadEmployee_CurrentCellChanged(sender, e)
    End Sub


    Public Sub randomPasswords()

        rndnumber = New Random

        number = rndnumber.Next(10000000, 80000000)

    End Sub


    Private Sub LBL_Search_Click(sender As Object, e As EventArgs) Handles LBL_Search.Click

        RanMumber = 1
        LBL_Employee.BackColor = Color.Transparent
        LBL_Attendance.BackColor = Color.Transparent
        lBL_Holiday.BackColor = Color.Transparent
        LBL_Report.BackColor = Color.Transparent
        LBL_Permission.BackColor = Color.Transparent
        LBL_UserRequest.BackColor = Color.Transparent
        LBL_Setting.BackColor = Color.Transparent
        LBL_RestoreDatabase.BackColor = Color.Transparent
        LBL_LogOut.BackColor = Color.Transparent
        LBL_About.BackColor = Color.Transparent

        Dim frm As New FormSearch
        frm.ShowDialog()
        If RanMumber = 1 Then
            LBL_Search.BackColor = Color.Black
          
        Else
            LBL_Search.BackColor = Color.Transparent
        End If

    End Sub


    Private Sub PicProfile_Click(sender As Object, e As EventArgs) Handles PicProfile.Click

    End Sub

    Private Sub PicProfile_MouseHover(sender As Object, e As EventArgs) Handles PicProfile.MouseHover

        tooltip.SetToolTip(PicProfile, "" + LBL_NAME.Text)

    End Sub
    Public Sub storeEmployeeAttendance()
        Cmd.ExecuteNonquery(" INSERT INTO TBL_STORE_EMPLOYEE (EMP_ID,EMP_CODE,EMP_NAME,ABSENT_DATE,ACTIVE) SELECT E.EMP_ID,E.EMP_CODE,E.EMP_NAME,'" & Now.ToShortDateString & "',E.ACTIVE   FROM TBL_EMPLOYEE E LEFT JOIN TBL_STORE_EMPLOYEE A ON E.EMP_ID = A.EMP_ID WHERE A.ABSENT_DATE IS NULL")

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs)
        storeEmployeeAttendance()
    End Sub
    Public Sub insertIntoUserLogin()
        Dim eid = Cmd.ExecuteScalar("SELECT   EMP_ID FROM TBL_EMPLOYEE ORDER BY EMP_ID DESC")

        Cmd.ExecuteNonquery("INSERT INTO SYS_USER (USER_CODE,EMP_ID,USER_NAME,PASSWORD,ROLE_ID,ACTIVE) VALUES('" & txtcode.Text & "','" & eid & "','" & txtname.Text & "','" & number & "','" & positionid & "',1)")

    End Sub
    Public Sub updateUserLogin()
        Try
            Cmd.ExecuteNonquery("UPDATE  SYS_USER SET USER_CODE='" & txtcode.Text & "',USER_NAME='" & txtname.Text & "',ROLE_ID='" & positionid & "',ACTIVE=1 WHERE EMP_ID='" & lblID.Text & "'")


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Public Sub updatetmpEmployee()

        Cmd.ExecuteNonquery("UPDATE TBL_STORE_EMPLOYEE SET EMP_CODE='" & txtcode.Text & "',EMP_NAME='" & txtname.Text & "' WHERE EMP_ID='" & lblID.Text & "'")
    End Sub

    Public Sub takeAttedaceEmployee()
        Cmd.ExecuteNonquery("UPDATE TBL_STORE_EMPLOYEE SET ACTIVE=0 WHERE EMP_CODE='" & ecode & "'")

    End Sub


    Public Sub loadholiday()
        Dgvholiday.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_HOLIDAY WHERE ACTIVE=1 ORDER BY DATE DESC")
        Label14.Text = Dgvholiday.RowCount.ToString() & "  Record(s)"
    End Sub

    Private Sub Label14_Click(sender As Object, e As EventArgs) Handles Label14.Click

    End Sub



    Private Sub HowToUseThisTimeManagementSystemToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Try


            If Now.ToShortTimeString <= CDate(FormLogin.mout) Then

                LoginMorning()
                Label22.Text = DataGridLoadData.RowCount.ToString() & " Record(s)"
                takeAttedaceEmployee()
            Else
                LoginAfternoon()
                Label22.Text = DataGridLoadData.RowCount.ToString() & " Record(s)"
                takeAttedaceEmployee()
                'LoginMorning()
            End If
        Catch ex As Exception


        End Try
    End Sub

    Private Sub ChangeLaguagesToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Try


            If Now.ToShortTimeString <= CDate(FormLogin.mout) Then
                LogOutMorning()
            ElseIf Now.ToShortTimeString > CDate(FormLogin.mout) Then
                LogOutAfternoon()
                'LogOutMorning()
            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Sub lblMessage_Click(sender As Object, e As EventArgs) Handles lblMessage.Click
        Me.TabMain.SelectedTab = TabUserRequest
        FormLogin.loadDataUserRequest()

    End Sub

    Private Sub DToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DToolStripMenuItem.Click
        lblMessage_Click(sender, e)
    End Sub

    Private Sub ShowAllUserRequestsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowAllUserRequestsToolStripMenuItem.Click
        'Select data from TBL_USER_REQUEST
        Try
            DgvUserRequest.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_USER_REQUEST")
            Label16.Text = DgvUserRequest.RowCount.ToString() & " Record(s)"
        Catch ex As Exception

        End Try
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RefreshToolStripMenuItem.Click
        lblMessage_Click(sender, e)

    End Sub

    Private Sub FullScreenToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles FullScreenToolStripMenuItem1.Click
        SplitContainer2.Panel1Collapsed = True
    End Sub

    Private Sub DefualtToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DefualtToolStripMenuItem.Click
        SplitContainer2.Panel1Collapsed = False
    End Sub

    Private Sub ToolStripButton4_Click(sender As Object, e As EventArgs) Handles ToolViewDeletedDocument.Click
        If lblMessage.Text = "" Then
            Exit Sub
        Else
            DgvUserRequest.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_USER_REQUEST WHERE ACTIVE=0")
            Label16.Text = DgvUserRequest.RowCount.ToString & " Record(s)"
        End If

    End Sub

    Private Sub ToolStripButton5_Click(sender As Object, e As EventArgs) Handles ToolRefresh.Click
        If lblMessage.Text = "" Then

            Exit Sub
        Else
            FormProgressLoad.ShowDialog()
            FormLogin.loadDataUserRequest()
            ToolStripButton6_Click(sender, e)
            txtUserCode.Focus()
        End If
    End Sub


    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Try
            If txtUserCode.Text = "" Then
                tooltip.IsBalloon = True
                Me.Highlighter1.SetHighlightColor(Me.txtUserCode, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Error Description."
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtUserCode, "Please in EmployeeCode")
                txtUserCode.SelectAll()
                txtUserCode.Focus()
                Exit Sub
            Else
                Me.Highlighter1.SetHighlightColor(Me.txtUserCode, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            End If

            If Me.txtDescription.Text.Length < 10 Or Me.txtDescription.Text.Length > 100 Then

                tooltip.IsBalloon = True

                tooltip.InitialDelay = 10
                tooltip.ToolTipTitle = "Error Description."
                tooltip.ToolTipIcon = ToolTipIcon.Error
                tooltip.SetToolTip(txtDescription, "Description must be between 10 to 100 charactors lenght.")
                txtDescription.SelectAll()
                txtDescription.Focus()

                Exit Sub

            Else

                If lblactive.Text = "False" Then
                    Exit Sub

                End If

                Cmd.ExecuteNonquery("INSERT INTO TBL_USER_REQUEST (REQUEST_CODE,REQUEST_NAME,PHONE,DESCRIPTION,REQUEST_DATE,ACTIVE) VALUES('" & txtUserCode.Text & "','" & txtUserName.Text & "','" & txtUserphone.Text & "','" & txtDescription.Text & "','" & DateTimePicker1.Text & "',1)")

                If FormLogin.perId = 1 Then
                    FormLogin.loadDataUserRequest()
                    Exit Sub
                Else


                    FormLogin.generalDataUserRequest()
                    txtDescription.Text = ""
                    txtDescription.Focus()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolUpdate.Click
        If lblMessage.Text = "" Then
            Exit Sub
        Else


            If txtUserCode.Text = "" Then
                Exit Sub
            Else

                If Me.txtDescription.Text.Length < 10 Or Me.txtDescription.Text.Length > 100 Then
                    tooltip.IsBalloon = True
                    tooltip.InitialDelay = 10
                    tooltip.ToolTipTitle = "error description"
                    tooltip.ToolTipIcon = ToolTipIcon.Error
                    tooltip.SetToolTip(txtDescription, "Description must be between 10 to 100 charactors lenght")
                    txtDescription.SelectAll()
                    txtDescription.Focus()
                    Exit Sub
                End If
                If lblactive.Text = "False" Then

                    Exit Sub
                End If

                Cmd.ExecuteNonquery("UPDATE TBL_USER_REQUEST SET DESCRIPTION='" & txtDescription.Text & "',REQUEST_DATE='" & DateTimePicker1.Text & "',ACTIVE=1 WHERE REQUEST_ID ='" & lblUserRequestid.Text & "'")
                FormLogin.loadDataUserRequest()
            End If
        End If
    End Sub

    Private Sub DgvUserRequest_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgvUserRequest.CellContentClick

    End Sub

    Private Sub DgvUserRequest_CurrentCellChanged(sender As Object, e As EventArgs) Handles DgvUserRequest.CurrentCellChanged
        Try
            For Each row As DataGridViewRow In DgvUserRequest.SelectedRows
                lblUserRequestid.Text = row.Cells("REQUEST_ID").Value
                txtUserCode.Text = row.Cells("REQUEST_CODE").Value
                txtUserName.Text = row.Cells("REQUEST_NAME").Value
                txtUserphone.Text = row.Cells("PHONE1").Value
                DateTimePicker1.Text = row.Cells("REQUEST_DATE").Value
                txtDescription.Text = row.Cells("DESCRIPTION1").Value
                lblactive.Text = row.Cells("ACTIVE2").Value

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub txtUserCode_Leave(sender As Object, e As EventArgs) Handles txtUserCode.Leave
        If txtUserName.Text = "" Then
            tooltip.IsBalloon = True
            Me.Highlighter1.SetHighlightColor(Me.txtUserCode, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Warning"
            tooltip.ToolTipIcon = ToolTipIcon.Warning
            tooltip.SetToolTip(txtUserCode, "Please input correct employee code.")
            txtUserCode.SelectAll()
            txtUserCode.Focus()

            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtUserCode, DevComponents.DotNetBar.Validator.eHighlightColor.None)
        End If
    End Sub

    Private Sub txtUserCode_TextChanged(sender As Object, e As EventArgs) Handles txtUserCode.TextChanged
        txtUserName.Text = Cmd.ExecuteScalar("SELECT EMP_NAME FROM TBL_EMPLOYEE WHERE EMP_CODE='" & txtUserCode.Text & "'")
        txtUserphone.Text = Cmd.ExecuteScalar("SELECT PHONE FROM TBL_EMPLOYEE WHERE EMP_CODE='" & txtUserCode.Text & "'")
    End Sub


    Private Sub ToolStripButton6_Click(sender As Object, e As EventArgs) Handles ToolCancel.Click
        If lblMessage.Text = "" Then
            Exit Sub
        End If
        txtUserCode.Text = ""
        txtDescription.Text = ""
        DateTimePicker1.Text = Now.ToShortDateString
    End Sub

    Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolClose.Click
        Me.Close()
    End Sub

    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolDelete.Click
        Try

            If lblMessage.Text = "" Then

                Exit Sub
            End If
            If lblactive.Text = "False" Then

                Exit Sub

            End If
            For Each row As DataGridViewRow In DgvUserRequest.SelectedRows
                Dim dr = row.Cells(0).Value
                Cmd.ExecuteNonquery("UPDATE TBL_USER_REQUEST SET ACTIVE=0 WHERE REQUEST_ID='" & dr & "'")

            Next
            txtUserCode.Text = ""
            txtDescription.Text = ""
            DateTimePicker1.Text = Now.ToShortDateString
            FormLogin.countsmsuser()
            FormLogin.loadDataUserRequest()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub ToolStripLabel1_Click(sender As Object, e As EventArgs) Handles ToolRestoreDB.Click
        Try

            If lblactive.Text = "" Then
                Exit Sub
            End If
            If lblactive.Text = "True" Then

                Exit Sub
            End If

            For Each row As DataGridViewRow In DgvUserRequest.SelectedRows
                Dim dr = row.Cells(0).Value
                Cmd.ExecuteNonquery("UPDATE TBL_USER_REQUEST SET ACTIVE=1 WHERE REQUEST_ID='" & dr & "'")
                FormLogin.countsmsuser()
            Next
            txtUserCode.Text = ""
            txtDescription.Text = ""
            DateTimePicker1.Text = Now.ToShortDateString
            DgvUserRequest.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_USER_REQUEST WHERE ACTIVE=0")
            Label16.Text = DgvUserRequest.RowCount.ToString() & " Record(s)"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Public Sub EmployeeReport()

        'Dim dt = Cmd.ExecuteDataTable("SELECT *FROM TBL_EMPLOYEE E JOIN  TBL_DEGREE D ON D.DEGREE_ID=E.DEGREE_ID JOIN TBL_POSITION P ON P.POSITION_ID=E.POSITION_ID JOIN TBL_SEX S ON S.SEX_ID=E.SEX_ID JOIN TBL_ADDRESS A ON A.ADDRESS_ID=E.ADDRESS_ID WHERE E.ACTIVE=1")
        'ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("DataSet1", dt))
        'ReportViewer1.LocalReport.ReportEmbeddedResource = ("TimeManagementSystem.ReportEmployee.rdlc")
        'Me.ReportViewer1.RefreshReport()
        'Me.ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout)
        'Me.ReportViewer1.ZoomMode = ZoomMode.FullPage
        '=============================================================
        Try


            Dim cryRpt As New ReportDocument

            Dim path = Application.StartupPath & "\Reports\ReportEmployee.rpt"
            cryRpt.Load(path)
            crviwer.ReportSource = cryRpt
            crviwer.Refresh()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try




    End Sub

    Public Sub Systemtime()
        Try
            txtmorin.Text = Cmd.ExecuteScalar("SElECT MORNING_IN FROM TBL_DEFUALT_TIME")
            txtmorout.Text = Cmd.ExecuteScalar("SElECT MORNING_OUT FROM TBL_DEFUALT_TIME")
            txtafterin.Text = Cmd.ExecuteScalar("SElECT AFTERNOON_IN FROM TBL_DEFUALT_TIME")
            txtafterout.Text = Cmd.ExecuteScalar("SElECT AFTERNOON_OUT FROM TBL_DEFUALT_TIME")
            txtrelax.Text = Cmd.ExecuteScalar("SElECT BREAK_TIME FROM TBL_DEFUALT_TIME")
            txtsysoff.Text = Cmd.ExecuteScalar(" SELECT SwithSystem FROM TBL_SWITH_SYSTEM ")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub Label28_Click(sender As Object, e As EventArgs) Handles Label28.Click

    End Sub

    Private Sub Button2_Click_1(sender As Object, e As EventArgs)

    End Sub

    Private Sub txtmorin_Leave(sender As Object, e As EventArgs) Handles txtmorin.Leave
        If Validator.IsTime(Me.txtmorin.Text) = False Then
            Me.Highlighter1.SetHighlightColor(Me.txtmorin, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.IsBalloon = True
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error Time."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtmorin, "Please input a correct time format.")
            txtmorin.SelectAll()
            txtmorin.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtmorin, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            Cmd.ExecuteNonquery("UPDATE TBL_DEFUALT_TIME SET MORNING_IN ='" & txtmorin.Text & "'")

            tooltip.SetToolTip(txtmorin, "")
        End If
    End Sub


    Private Sub txtmorin_TextChanged(sender As Object, e As EventArgs) Handles txtmorin.TextChanged

    End Sub

    Private Sub txtmorout_Leave(sender As Object, e As EventArgs) Handles txtmorout.Leave
        If Validator.IsTime(Me.txtmorout.Text) = False Then

            tooltip.IsBalloon = True
            Me.Highlighter1.SetHighlightColor(Me.txtmorout, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error Time."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtmorout, "Please input a correct time format.")
            txtmorout.SelectAll()
            txtmorout.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtmorout, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            Cmd.ExecuteNonquery("UPDATE TBL_DEFUALT_TIME SET MORNING_OUT ='" & txtmorout.Text & "'")
            tooltip.SetToolTip(txtmorout, "")
        End If
    End Sub

    Private Sub txtrelax_Leave(sender As Object, e As EventArgs) Handles txtrelax.Leave
        If Validator.IsTime(Me.txtrelax.Text) = False Then
            Me.Highlighter1.SetHighlightColor(Me.txtrelax, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.IsBalloon = True
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error Time."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtrelax, "Please input a correct time format.")
            txtrelax.SelectAll()
            txtrelax.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtrelax, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            Cmd.ExecuteNonquery("UPDATE TBL_DEFUALT_TIME SET BREAK_TIME ='" & txtrelax.Text & "'")
            tooltip.SetToolTip(txtrelax, "")
        End If
    End Sub

    Private Sub txtafterin_Leave(sender As Object, e As EventArgs) Handles txtafterin.Leave
        If Validator.IsTime(Me.txtafterin.Text) = False Then
            Me.Highlighter1.SetHighlightColor(Me.txtafterin, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.IsBalloon = True
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error Time."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtafterin, "Please input a correct time format.")
            txtafterin.SelectAll()
            txtafterin.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtafterin, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            Cmd.ExecuteNonquery("UPDATE TBL_DEFUALT_TIME SET AFTERNOON_IN ='" & txtafterin.Text & "'")
            tooltip.SetToolTip(txtafterin, "")
        End If
    End Sub


    Private Sub txtafterout_Leave(sender As Object, e As EventArgs) Handles txtafterout.Leave
        If Validator.IsTime(Me.txtafterout.Text) = False Then
            Me.Highlighter1.SetHighlightColor(Me.txtafterout, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.IsBalloon = True
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error Time."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtafterout, "Please input a correct time format.")
            txtafterout.SelectAll()
            txtafterout.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtafterout, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            Cmd.ExecuteNonquery("UPDATE TBL_DEFUALT_TIME SET AFTERNOON_OUT ='" & txtafterout.Text & "'")
            tooltip.SetToolTip(txtafterout, "")
        End If
    End Sub


    Private Sub txtsysoff_Leave(sender As Object, e As EventArgs) Handles txtsysoff.Leave
        If Validator.IsTime(Me.txtsysoff.Text) = False Then
            Me.Highlighter1.SetHighlightColor(Me.txtsysoff, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.IsBalloon = True
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error Time."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtsysoff, "Please input a correct time format.")
            txtsysoff.SelectAll()
            txtsysoff.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtsysoff, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            Cmd.ExecuteNonquery("UPDATE TBL_SWITH_SYSTEM SET SwithSystem ='" & txtsysoff.Text & "'")
            tooltip.SetToolTip(txtsysoff, "")

        End If
    End Sub

    Private Sub txtcode_Leave(sender As Object, e As EventArgs) Handles txtcode.Leave
        If txtcode.Text <> "" Then


            Me.Highlighter1.SetHighlightColor(Me.txtcode, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(txtcode, "")
        End If
    End Sub



    Private Sub txtname_Leave(sender As Object, e As EventArgs) Handles txtname.Leave
        If txtname.Text <> "" Then


            Me.Highlighter1.SetHighlightColor(Me.txtname, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(txtname, "")
        End If
        If Me.txtname.Text.Length < 2 Or Me.txtname.Text.Length > 20 Then

            ValidEmployeeLenght()

            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtname, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(txtname, "")
        End If
    End Sub

    Private Sub txtname_TextChanged(sender As Object, e As EventArgs) Handles txtname.TextChanged

    End Sub

    Private Sub txtphone_Leave(sender As Object, e As EventArgs) Handles txtphone.Leave


        If Validator.IsPhone(Me.txtphone.Text) = False Then

            tooltip.IsBalloon = True
            Me.Highlighter1.SetHighlightColor(Me.txtphone, DevComponents.DotNetBar.Validator.eHighlightColor.Red)
            tooltip.InitialDelay = 10
            tooltip.ToolTipTitle = "Error phone number."
            tooltip.ToolTipIcon = ToolTipIcon.Error
            tooltip.SetToolTip(txtphone, "Please input a correct phone number format.")
            txtphone.SelectAll()
            txtphone.Focus()
            Exit Sub
        Else
            Me.Highlighter1.SetHighlightColor(Me.txtphone, DevComponents.DotNetBar.Validator.eHighlightColor.None)
            tooltip.SetToolTip(txtphone, "")
        End If
    End Sub

    Private Sub txtphone_TextChanged(sender As Object, e As EventArgs) Handles txtphone.TextChanged

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs)

    End Sub

    Public Sub CopyPasswordToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Add.Click
        Dim frm As New FormAddHoliday
        frm.ShowDialog()

    End Sub


    Private Sub txtafterin_TextChanged(sender As Object, e As EventArgs) Handles txtafterin.TextChanged
        txtafterin.Copy()

    End Sub

    Private Sub Panel3_Paint(sender As Object, e As PaintEventArgs) Handles Panel3.Paint

    End Sub

    Private Sub MetrolClassicToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MetrolClassicToolStripMenuItem.Click

        FormProgressLoad.ShowDialog()
        Cmd.ExecuteNonquery("UPDATE TBL_BACKGROUND SET BACKGROUND_CODE=1 WHERE BACKGROUND_ID=1")
    End Sub

    Private Sub MetrolTerminalToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MetrolTerminalToolStripMenuItem.Click

        FormProgressLoad.ShowDialog()
        Cmd.ExecuteNonquery("UPDATE TBL_BACKGROUND SET BACKGROUND_CODE=2 WHERE BACKGROUND_ID=1")
    End Sub

    Private Sub MetrolWhirteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MetrolWhirteToolStripMenuItem.Click
        FormProgressLoad.ShowDialog()
        Cmd.ExecuteNonquery("UPDATE TBL_BACKGROUND SET BACKGROUND_CODE=3 WHERE BACKGROUND_ID=1")
    End Sub

    Private Sub AToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AToolStripMenuItem.Click
        FormProgressLoad.ShowDialog()
        Cmd.ExecuteNonquery("UPDATE TBL_BACKGROUND SET BACKGROUND_CODE=4 WHERE BACKGROUND_ID=1")
    End Sub

    Private Sub AbstracMultiBlureToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AbstracMultiBlureToolStripMenuItem.Click
        FormProgressLoad.ShowDialog()
        Cmd.ExecuteNonquery("UPDATE TBL_BACKGROUND SET BACKGROUND_CODE=5 WHERE BACKGROUND_ID=1")
    End Sub

    Public Sub FormatDataGridViews()
        UI.FormatDataGrid(DgLoadEmployee)
        UI.FormatDataGrid(DgvAttendace)
        UI.FormatDataGrid(Dgvholiday)
        UI.FormatDataGrid(DgvUserLogin)
        UI.FormatDataGrid(DgvUserRequest)
        UI.FormatDataGrid(DgvRestoreDb)
        UI.FormatDataGrid(DataGridLoadData)
    End Sub

    Private Sub txtDataGridViewAlternativeColor_MouseClick(sender As Object, e As MouseEventArgs) Handles txtDataGridViewAlternativeColor.MouseClick


        Dim cd As New ColorDialog()
        cd.Color = Me.txtDataGridViewAlternativeColor.BackColor
        If (cd.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Me.txtDataGridViewAlternativeColor.BackColor = cd.Color
        End If
    End Sub

    Private Sub txtDataGridViwFontName_MouseClick(sender As Object, e As MouseEventArgs) Handles txtDataGridViwFontName.MouseClick
        Dim fd As New FontDialog()

        fd.Font = Me.txtDataGridViwFontName.Font


        If fd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.txtDataGridViwFontName.Font = fd.Font


        End If
    End Sub

    Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles BtnSaveSetting.Click


        My.Settings.DataGridViewAlternativeColor = Me.txtDataGridViewAlternativeColor.BackColor
        My.Settings.DataGridViweRowHeight = Me.txtDataGridViewRowHeigt.Text
        My.Settings.DataGridViweFont = Me.txtDataGridViwFontName.Font

        My.Settings.Languguage = Me.cboLanguage.Text
        My.Settings.Save()
        MsgBox("Setting update successful", vbOKOnly, "Time Management System")
        'Me.Close()
    End Sub


    Private Sub txtDataGridViwFontName_TextChanged(sender As Object, e As EventArgs) Handles txtDataGridViwFontName.TextChanged

    End Sub

    Private Sub DgLoadEmployee_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DgLoadEmployee.CellContentClick

    End Sub

    Private Sub PanelNopicture_MouseClick(sender As Object, e As MouseEventArgs) Handles PanelNopicture.MouseClick
        PicEmployee_Click(sender, e)
        If PicEmployee.Image Is Nothing Then
            PicEmployee.SendToBack()
            PanelNopicture.Show()
        Else
            PanelNopicture.Hide()
        End If

    End Sub

    Private Sub PanelNopicture_Paint(sender As Object, e As PaintEventArgs) Handles PanelNopicture.Paint

    End Sub

    Private Sub txtDataGridViewRowHeigt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDataGridViewRowHeigt.KeyPress
        Select Case Asc(e.KeyChar)
            Case 48 To 57, 8
            Case Else
                e.Handled = True
        End Select
    End Sub

    Private Sub txtDataGridViewRowHeigt_Leave(sender As Object, e As EventArgs) Handles txtDataGridViewRowHeigt.Leave
        If txtDataGridViewRowHeigt.Text = "" Then
            BtnSaveSetting.Enabled = False
        Else
            BtnSaveSetting.Enabled = True
        End If
    End Sub



    Private Sub DgvUserLogin_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgvUserLogin.CellValueChanged
        Try
            For Each row As DataGridViewRow In DgvUserLogin.SelectedRows

                Cmd.ExecuteNonquery("UPDATE SYS_USER SET PASSWORD='" & row.Cells(4).Value & "' WHERE USER_ID='" & row.Cells(0).Value & "' ")

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


    Private Sub BtnChekIn_Click(sender As Object, e As EventArgs) Handles BtnChekIn.Click
        Try


            If Now.ToShortTimeString <= CDate(FormLogin.mout) Then
                LoginMorning()
                Label22.Text = DataGridLoadData.RowCount.ToString() & " Record(s)"
                takeAttedaceEmployee()
            Else
                LoginAfternoon()

                Label22.Text = DataGridLoadData.RowCount.ToString() & " Record(s)"
                takeAttedaceEmployee()

            End If
        Catch ex As Exception


        End Try
    End Sub

    Private Sub BtnChekOut_Click(sender As Object, e As EventArgs) Handles BtnChekOut.Click
        Try


            If Now.ToShortTimeString <= CDate(FormLogin.mout) Then
                LogOutMorning()

            ElseIf Now.ToShortTimeString > CDate(FormLogin.mout) Then
                LogOutAfternoon()


            End If
        Catch ex As Exception

        End Try
    End Sub



    Private Sub Dgvholiday_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles Dgvholiday.CellValueChanged
        Try
            For Each row As DataGridViewRow In Dgvholiday.SelectedRows

                Cmd.ExecuteNonquery("UPDATE TBL_HOLIDAY SET DATE='" & row.Cells(1).Value & "',DESCRIPTION='" & row.Cells(2).Value & "' WHERE NO ='" & row.Cells(0).Value & "' ")

            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub RemoveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem.Click
        Try
            For Each row As DataGridViewRow In Dgvholiday.SelectedRows

                Cmd.ExecuteNonquery("UPDATE TBL_HOLIDAY SET ACTIVE=0  WHERE NO='" & row.Cells(0).Value & "'")
            Next
            loadholiday()
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub RestoreToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles RestoreToolStripMenuItem1.Click

        Dgvholiday.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_HOLIDAY WHERE ACTIVE=0")
        Label14.Text = Dgvholiday.RowCount.ToString() & "  Record(s)"
        If Dgvholiday.Rows.Count.ToString() = 0 Then
            loadholiday()
        Else
            Add.Visible = False
            RemoveToolStripMenuItem.Visible = False
            RestoreAll.Visible = True
            Restore.Visible = True
        End If



    End Sub

    Private Sub Restore_Click(sender As Object, e As EventArgs) Handles Restore.Click
        FormProgressLoad.ShowDialog()
        Try
            For Each row As DataGridViewRow In Dgvholiday.SelectedRows

                Cmd.ExecuteNonquery("UPDATE TBL_HOLIDAY SET ACTIVE=1  WHERE NO='" & row.Cells(0).Value & "'")

            Next
            Dgvholiday.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_HOLIDAY WHERE ACTIVE=0")
            Label14.Text = Dgvholiday.RowCount.ToString() & "  Record(s)"


            If Dgvholiday.Rows.Count.ToString() = 0 Then
                loadholiday()
                Add.Visible = True
                RemoveToolStripMenuItem.Visible = True
                RestoreAll.Visible = False
                Restore.Visible = False

            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try


    End Sub

    Private Sub RestoreAll_Click(sender As Object, e As EventArgs) Handles RestoreAll.Click
        FormProgressLoad.ShowDialog()
        Try
            For Each row As DataGridViewRow In Dgvholiday.Rows

                Cmd.ExecuteNonquery("UPDATE TBL_HOLIDAY SET ACTIVE=1  WHERE NO='" & row.Cells(0).Value & "'")
            Next
            loadholiday()
            RestoreAll.Visible = False
            Restore.Visible = False
            Add.Visible = True
            RemoveToolStripMenuItem.Visible = True
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Private Sub RefreshToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles RefreshToolStripMenuItem1.Click
        FormProgressLoad.ShowDialog()
        Add.Visible = True
        RemoveToolStripMenuItem.Visible = True
        RestoreAll.Visible = False
        Restore.Visible = False
        loadholiday()
    End Sub

    Private Sub RemoveToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles RemoveToolStripMenuItem1.Click
        Try
            For Each row As DataGridViewRow In DgvAttendace.SelectedRows
                Cmd.ExecuteNonquery("UPDATE TBL_ATTENDENT SET ACTIVE=0 WHERE ATTENTDENT_ID='" & row.Cells(0).Value & "'")
            Next
            LoadAttendace()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub RestoreDatabaseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreDatabaseToolStripMenuItem.Click

        DgvAttendace.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_ATTENDENT WHERE ACTIVE=0")
        Label12.Text = DgvAttendace.RowCount.ToString() & "  Record(s)"
        If DgvAttendace.Rows.Count.ToString() = 0 Then
            LoadAttendace()
        Else

            RemoveToolStripMenuItem1.Visible = False
            RestoreToolStripMenuItem2.Visible = True
            RestoreAllToolStripMenuItem1.Visible = True
        End If

    End Sub

    Private Sub RestoreToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles RestoreToolStripMenuItem2.Click
        FormProgressLoad.ShowDialog()
        Try
            For Each row As DataGridViewRow In DgvAttendace.SelectedRows

                Cmd.ExecuteNonquery("UPDATE TBL_ATTENDENT SET ACTIVE=1  WHERE ATTENTDENT_ID='" & row.Cells(0).Value & "'")

            Next
            DgvAttendace.DataSource = Cmd.ExecuteDataTable("SELECT * FROM TBL_ATTENDENT WHERE ACTIVE=0")
            Label12.Text = DgvAttendace.RowCount.ToString() & "  Record(s)"


            If DgvAttendace.Rows.Count.ToString() = 0 Then
                LoadAttendace()
                RemoveToolStripMenuItem1.Visible = True
                RestoreToolStripMenuItem2.Visible = False
                RestoreAllToolStripMenuItem1.Visible = False
            End If
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub

    Private Sub RestoreAllToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles RestoreAllToolStripMenuItem1.Click
        FormProgressLoad.ShowDialog()
        Try
            For Each row As DataGridViewRow In DgvAttendace.Rows

                Cmd.ExecuteNonquery("UPDATE TBL_ATTENDENT SET ACTIVE=1  WHERE ATTENTDENT_ID='" & row.Cells(0).Value & "'")
            Next
            LoadAttendace()
            RestoreToolStripMenuItem2.Visible = False
            RestoreAllToolStripMenuItem1.Visible = False
            RemoveToolStripMenuItem1.Visible = True

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub


    Private Sub RefreshToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles RefreshToolStripMenuItem2.Click
        FormProgressLoad.ShowDialog()
        RemoveToolStripMenuItem1.Visible = True
        RestoreToolStripMenuItem2.Visible = False
        RestoreAllToolStripMenuItem1.Visible = False

        LoadAttendace()
    End Sub
    Public Sub MainTab()



        If TabMain.SelectedTab Is TabHome Then

            LBL_Employee.BackColor = Color.Transparent
            LBL_Attendance.BackColor = Color.Transparent
            lBL_Holiday.BackColor = Color.Transparent
            LBL_Report.BackColor = Color.Transparent
            LBL_Permission.BackColor = Color.Transparent
            LBL_UserRequest.BackColor = Color.Transparent
            LBL_Setting.BackColor = Color.Transparent
            LBL_RestoreDatabase.BackColor = Color.Transparent
            LBL_Logo.BackColor = Color.Transparent
            LBL_LogOut.BackColor = Color.Transparent
        End If

    End Sub

    
    Private Sub Button1_Click_3(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sum As Int16 = 0
        DataGridLoadData.MultiSelect = True
        For i = 0 To DataGridLoadData.Rows.Count - 1

            If DataGridLoadData.Rows(i).Cells("DATETIME").Value = Now.ToShortDateString Then

                sum = sum + 1
                DataGridLoadData.Rows(i).Selected = True
            End If

        Next i

        MsgBox(sum.ToString + "   Total of Employees are Working ")
     





    End Sub

    Private Sub Button2_Click_2(sender As Object, e As EventArgs) Handles Button2.Click
        FormDgvDemo.ShowDialog()
    End Sub

    Private Sub ContextMenuRestoreDatabase_Opening(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles ContextMenuRestoreDatabase.Opening

    End Sub

    Private Sub CToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CToolStripMenuItem.Click
        EmployeeReport()
    End Sub

    Private Sub TimeWorkToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TimeWorkToolStripMenuItem.Click
        TimeWorkReport()
    End Sub
    Public Sub TimeWorkReport()
        Try


            Dim cryRpt As New ReportDocument

            Dim path = Application.StartupPath & "\Reports\TimeWorkReport.rpt"
            cryRpt.Load(path)
            crviwer.ReportSource = cryRpt
            crviwer.Refresh()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub AttendaceReport()
        Try


            Dim cryRpt As New ReportDocument

            Dim path = Application.StartupPath & "\Reports\AttendaceReport.rpt"
            cryRpt.Load(path)
            crviwer.ReportSource = cryRpt
            crviwer.Refresh()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub AttendaceToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AttendaceToolStripMenuItem.Click
        AttendaceReport()
    End Sub

    Private Sub ViewReportsAsFullScreenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewReportsAsFullScreenToolStripMenuItem.Click
        Me.Hide()

        FormReport.ShowDialog()
    End Sub
End Class
